package Evaluation;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;

import javax.print.attribute.HashAttributeSet;

public class HumanEvaluation {
	private BufferedReader br;
	private HashMap<String, Integer[]> results;
	private String currentMWE1;
	private String currentMWE;

	private BufferedWriter bw;
	private HashSet<String> MWEs;

	public HumanEvaluation() throws IOException {
		results = new HashMap<String, Integer[]>();
		MWEs = new HashSet<String>();
		bw = new BufferedWriter(new FileWriter(new File("out/correct")));
	}

	public void readEvaluations() throws Exception {

		File dir = new File("Evaluation/");
		File[] files = dir.listFiles();
		String line = "";
		String lineTransformed = "";
		String[] split;
		int counter;
		for (File file : files) {
			if (file.getName().startsWith("!!!")) {
				continue;
			}
			currentMWE1 = file.getName().replace("_top10.txt", "");
			currentMWE = currentMWE1.split("__")[0]
					.replaceFirst("results_", "").replaceAll("_", " ");
			MWEs.add(currentMWE.split("__")[0].replaceFirst("results_", "")
					.replaceAll("_", " "));
			System.out.println(currentMWE.split("__")[0].replaceFirst(
					"results_", "").replaceAll("_", " "));
			br = new BufferedReader(new FileReader(file));
			while ((line = br.readLine()) != null) {
				counter = 0;
				if (line.contains("(") && line.contains(")")) {
					lineTransformed = line.trim().replaceAll("\\(", "0")
							.replaceAll("\\)", "").replaceAll("\\s+", " ");
					split = lineTransformed.split(" ");
					if (split[1].contains("x")) {
						bw.write(currentMWE.replaceAll(" ", "_") + " " + split[0] + "\n");
						updateResults(0);
						counter++;
					}
					if (split[2].contains("x")) {
						updateResults(1);
						counter++;
					}
					if (split[3].contains("x")) {
						updateResults(2);
						counter++;
					}
					if (counter != 1) {
						throw new Exception("number of annotations incorrect "
								+ line);
					}

				}
			}
		}
		printResults();
		addTargetParaphrase();
		bw.flush();
		bw.close();

	}

	public void updateResults(int pos) {
		if (results.containsKey(currentMWE)) {
			Integer[] array = results.get(currentMWE);
			if (array[pos] == null)
				System.out.println("NULLLL");
			array[pos] = array[pos] + 1;
			results.put(currentMWE, array);
		} else {

			Integer[] array = new Integer[] { 0, 0, 0 };
			array[pos] = 1;
			results.put(currentMWE, array);

		}
	}

	public void printSplitLine(String[] split) {
		String str = "";
		for (String s : split) {
			str += ">" + s + "<";
		}
		System.out.println(str);
	}

	public void printResults() throws IOException {
		String str = "";
		for (String s : results.keySet()) {
			str = "";
			for (int i = 0; i < results.get(s).length; i++) {
				str += "pos " + i + " : " + results.get(s)[i] + "\n";
			}
			System.out.println(s);
			System.out.println(str);
		}
	}

	public void addTargetParaphrase() throws IOException {
		BufferedReader br2 = new BufferedReader(new FileReader(new File(
				"data/idiomsParsedSort")));
		String line = "";
		String[] split;
		String[] targets;
		String t = "";
		while ((line = br2.readLine()) != null) {
			split = line.split("/");
			if (MWEs.contains(split[0].trim())) {
				targets = split[2].split(",");
				for (String s : targets) {
					t = split[0].trim().replaceAll(" ","_") + " "
							+ s.replaceAll("\\d+", "").replaceAll("_", "").trim();
					bw.write(t+"\n");
				}
			}
		}
	}
}
